/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.service.impl;

import com.ktjiaoyu.news.dao.NewsCollectDao;
import com.ktjiaoyu.news.dao.impl.NewsCollectDaoImpl;
import com.ktjiaoyu.news.entity.NewsCollect;
import com.ktjiaoyu.news.service.NewsCollectService;
import com.ktjiaoyu.news.utils.Page;

import java.util.List;

/**
 * 会员可以收藏新闻方便后续查看 Service接口实现
 * @author 科泰集团教研团队
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 * @version 1.0.0
 */
public class NewsCollectServiceImpl implements NewsCollectService {

    private NewsCollectDao newsCollectDao = new NewsCollectDaoImpl();
    @Override
    public Page<NewsCollect> findPage(NewsCollect entity, int pageNo, int pageSize) {
        Page<NewsCollect> page = new Page<>();
        // 获得总数量
        int count = newsCollectDao.count(entity);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 设置总数量，计算总页数
        page.setTotalCount(count);
        List<NewsCollect> list = newsCollectDao.getPage(entity, pageNo, pageSize);
        page.setData(list);
        return page;
    }

    @Override
    public List<NewsCollect> findAll() {
        return newsCollectDao.getAll();
    }

    @Override
    public NewsCollect findById(Long id) {
        return newsCollectDao.getById(id);
    }

    @Override
    public boolean saveOrUpdate(NewsCollect entity) {
        if(entity.getId() != null){
            // 如果有ID就是修改
            return newsCollectDao.update(entity) > 0;
        }
        // 没有id就是新增
        return newsCollectDao.insert(entity) > 0;
    }

    @Override
    public boolean importBatch(NewsCollect... entities) {
        return newsCollectDao.insertBatch(entities) != null;
    }


    @Override
    public boolean removeById(Long id) {
        return newsCollectDao.deleteById(id) > 0;
    }

    @Override
    public boolean removeByEntity(NewsCollect entity) {
        return newsCollectDao.deleteByEntity(entity) > 0;
    }

    @Override
    public boolean removeBatch(Long... ids) {
        return newsCollectDao.deleteBatch(ids) != null;
    }
}
